function [x,y,typ]=playsound_int(job,arg1,arg2)

x=[];y=[];typ=[]

select job
  case 'plot' then
    standard_draw(arg1)
  case 'getinputs' then
    [x,y,typ]=standard_inputs(arg1)
  case 'getoutputs' then
    [x,y,typ]=standard_outputs(arg1)
  case 'getorigin' then
    [x,y]=standard_origin(arg1)
  case 'set' then
    //set the parameters of play sound block, 
    //parameters:
    //sample rate,bits per sample,buffer size, channel num
    x=arg1;
    graphics=arg1.graphics;model=arg1.model;
    exprs=graphics.exprs;

    while %t do
      //get parameters
      [ok, fs, bits, buffersize, channelnum, exprs]=getvalue(...
      'Play Sound block',...
      ['sample rate'; 'bits per sample'; 'buffer size'; 'channel number(1 or 2)'],...
      list('vec',1,'vec',1,'vec',1,'vec',1),exprs)

      if ~ok then break,end;

      //check validity of parameters
      if fs <= 0 then
        message('sample rate must be a positive integer');
        ok = %f;
      elseif bits <= 0 then
        message('bits per sample must be a positive integer');
        ok = %f;
      elseif buffersize <= 0 then
        message('buffersize must be a positive integer');
        ok = %f;
      elseif channelnum ~= 1 & channelnum ~= 2 then
        message('channelnum must be 1 or 2');
        ok = %f;
      end

      //write back parameters
      if ok then
        graphics.exprs=exprs
        if(channelnum == 1) then
          model.in = -1;
        else
          [model,graphics,ok]=check_io(model,graphics,[-1;-2],[],1,[])
        end
        model.ipar = [fs, bits, buffersize, channelnum];
        model.dstate = zeros(channelnum, buffersize+1);
        x.graphics=graphics;x.model=model
        break;
      end
    end //while
  
  case 'define' then
    fs = 22050;
    bits = 16;
    buffersize = 2205;
    channelnum = 1;
    exprs=[string(fs), string(bits), string(buffersize), string(channelnum)];
    model=scicos_model()
    model.sim=list('playsound_com',5)
    model.in = -1;
    model.out = []
    model.ipar = [fs, bits, buffersize, channelnum];
    //dstate the sample buffer, each row represent one channel, last element is index
    model.dstate = zeros(channelnum, buffersize+1);
    model.blocktype='d'
    model.evtin = [1];
    model.dep_ut=[%f %f]
    gr_i='xstringb(orig(1),orig(2),[''Play sound''],sz(1),sz(2),''fill'')';
    x=standard_define([2.5 2.5],model,exprs,gr_i)
  end
endfunction

